package com.smartstate.IOC.controller;

import com.smartstate.IOC.entity.DeviceCarePlan;
import com.smartstate.IOC.entity.EquipMent;
import com.smartstate.IOC.service.EquipmentService;
import com.smartstate.IOC.util.StringDateUtills;
import com.smartstate.IOC.vo.EquipMentVidioVo;
import com.smartstate.IOC.vo.MoudelEquipmentVo;
import com.smartstate.common.web.httpResult.ResultVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/equipment")
@Api(tags = "设备控制层")
@Slf4j
public class EquipmentController implements Serializable {

    @Resource
    private EquipmentService equipmentService;

    @RequestMapping(value = "/getMoudelEquipments",method = RequestMethod.GET)
    @ApiOperation(value = "综合态势-设备类型统计，获取各楼栋的设备数量")
    public ResultVO<Map> getMoudelEquipments(){

        Map<String, Integer> result;
        try{
            result = equipmentService.getMoudelEquipments();
        }catch(Exception e){
            log.info("获取各楼栋的设备数量时发生错误：{}", e);
            return ResultVO.error("无法正常获取数据！");
        }
        return ResultVO.success(result);
    }

    @RequestMapping(value = "/getOutputSummary",method = RequestMethod.GET)
    @ApiOperation(value = "综合态势-园区产量，设备产出汇总")
    public ResultVO<Map<String,List<Map<String,Object>>>> getOutputSummary(){
        //每月每台设备固定生产20000个
        Long outputNum = 20000L;
        //<设备Id,List<Map<月份，本月生产数量>>>
        Map<String,List<Map<String,Object>>> result = new HashMap<>();
        for(int equipmentNum= 1; equipmentNum < 4; equipmentNum ++){
            Calendar calendar = Calendar.getInstance();
            int currMoth =  calendar.get(Calendar.MONTH);
            List<Map<String, Object>> monthOut = Lists.newArrayList();
            String monthStr;
            for(int monthNum = (currMoth-5); monthNum <= currMoth ; monthNum ++ ){
                Map<String,Object> monthOutput = new HashMap<>();
                calendar.set(Calendar.MONTH,monthNum);
                monthStr = StringDateUtills.getMonthStyleStrFromDateTime(new Date(calendar.getTimeInMillis()));
                monthOutput.put(monthStr,outputNum);
                monthOut.add(monthOutput);
            }
            result.put("producer" + equipmentNum, monthOut);
        }
        return ResultVO.success(result);
    }

    @RequestMapping(value = "/getOnlineStateEquipmets", method = RequestMethod.GET)
    @ApiOperation(value = "能源态势-用电状态统计，获取在线状态设备数量统计")
    public Map<String,Map<String, Object>> getOnlineStateEquipmets(){
        return equipmentService.getOnlineStateEquipmets();
    }

    @RequestMapping(value = "/getTypeEquipments", method = RequestMethod.GET)
    @ApiOperation(value = "运维态势-楼宇设备类型统计")
    public ResultVO<List<Map<String, Object>>> getTypeEquipments(){
        MoudelEquipmentVo rootNode = equipmentService.getMoudelEquipmentTree("1");
        List<MoudelEquipmentVo> equipmentVos = equipmentService.getTargetSubMoudels(rootNode);
        equipmentVos = equipmentVos.stream().filter(e -> StringUtils.isNotBlank(e.getDeviceId()) && StringUtils.isNotBlank(e.getEquipmentTypeName())).collect(Collectors.toList());
        Map tempResult = equipmentVos.stream().collect(Collectors.groupingBy(MoudelEquipmentVo :: getEquipmentTypeName, Collectors.counting()));

        List<Map<String, Object>> result = Lists.newArrayList();
        tempResult.entrySet().stream().forEach(e -> {
            Map.Entry entry = (Map.Entry) e;
            Map<String, Object> temp = new HashMap<>();
            temp.put("deviceName", entry.getKey());
            temp.put("deviceCountNum", entry.getValue());
            result.add(temp);
        });
        return ResultVO.success(result);
    }

    @RequestMapping(value = "/getEquipmentCarePlans", method = RequestMethod.GET)
    @ApiOperation(value = "运维态势-设备剩余保养时间计时列表")
    public List<DeviceCarePlan> getEquipmentCarePlans() {
        return equipmentService.getEquipmentCarePlans();
    }

    @RequestMapping(value = "/getTeamCountInfos", method = RequestMethod.GET)
    @ApiOperation(value = "运维态势-班组信息列表")
    public List<Map<String,Object>> getTeamCountInfos(){
        return equipmentService.getTeamCountInfos();
    }

    @GetMapping("/getDeviceVidio")
    @ApiOperation(value = "环境设备总数")
    public Map<String, Integer> getDeviceVidio(){
        return equipmentService.getDeviceVidio();
    }


    @GetMapping ("/getDeviceObj")
    @ApiOperation(value = "查询今日设备离线统计")
    public Map<String, List<Integer>> getDeviceObj(){
        return equipmentService.getDeviceObj();
    }

    @PostMapping("/getDeviceVidioTotal")
    @ApiOperation(value = "查询类型为看板的设备")
    public List<EquipMentVidioVo> getDeviceVidioTotal(){
        return equipmentService.getDeviceVidioTotal();
    }

    @PostMapping("/getVidioEquipment")
    @ApiOperation(value = "查询类型为看板的设备")
    public Map<String, Object> getVidioEquipment(@RequestBody EquipMent equipMent){
        return equipmentService.getVidioEquipment(equipMent);
    }

    @GetMapping ("/getCameraChannelUrls")
    @ApiOperation(value = "获取视频设备对应频道url集")
    public Map<String, String> getCameraChannelUrls(){
        Map<String, String> result = equipmentService.getCameraChannelUrls();
        if(CollectionUtils.isEmpty(result.entrySet())){
            return null;
        }
        return result;
    }

    @PostMapping ("/getChannelUrlByCameraId")
    @ApiOperation(value = "根据摄像头id获取视频设备对应频道url")
    @ApiParam(name = "cameraId", value = "String")
    public ResultVO<String> getChannelUrlByCameraId(@RequestParam String  cameraId){
        if(StringUtils.isBlank(cameraId)){
            return ResultVO.error("摄像头ID未传，请检查你的参数。");
        }
        String channelId = equipmentService.getChannelUrlByCameraId(cameraId);
        if(StringUtils.isBlank(channelId)){
            return ResultVO.error("请联系管理员查看对应设备视频频道配置。");
        }
        return ResultVO.success(channelId);
    }


    @GetMapping("/selectAlarmEvent")
    @ApiOperation(value = "环境姿态-环境设备总数")
    public Map<String, Integer> getAlarmEvent(){
        return equipmentService.getAlarmEvent();
    }

    @GetMapping("/getDeviceVideo")
    @ApiOperation(value = "安防态势视频设备类型统计")
    public Map<String, Integer> getDeviceVideo(){
        return equipmentService.getDeviceVideo();
    }

    @GetMapping("/getEnvironmentEquipmentInfo")
    @ApiOperation(value = "综合态势-环境数据")
    public Map<String, Object> getEnvironmentEquipmentInfo(){
        Map<String, Object> info = new HashMap<>();
        //温度
        info.put("temperature", 27);
        //湿度
        info.put("humidity", 52);
        //噪声
        info.put("noise", 32);
        //大气压力
        info.put("pressure", 101);
        info.put("PM2.5", 53);
        info.put("PM10", 0.12);
        return info;
    }

}
